# -*- coding: UTF-8 -*-
from leetcode_helper import TestcaseHelper, SolutionBase, ListNode, array_to_list, list_to_array


class Solution(SolutionBase):
    # 解题的方法，命名和Leetcode的保持一致即可
    def mergeTwoLists_1(self, l1: ListNode, l2: ListNode) -> ListNode:
        if not l1:
            return l2
        if not l2:
            return l1
        if l1.val < l2.val:
            l1.next = self.mergeTwoLists(l1.next, l2)
            return l1
        else:
            l2.next = self.mergeTwoLists(l2.next, l1)
            return l2

    def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
        dummy = ListNode(0)
        curr = dummy

        while l1 and l2:
            if l1.val < l2.val:
                curr.next = l1
                curr = l1
                l1 = l1.next
            else:
                curr.next = l2
                curr = l2
                l2 = l2.next

        if l1:
            curr.next = l1
        if l2:
            curr.next = l2

        return dummy.next


if __name__ == '__main__':
    solution = Solution()

    # 按照leetcode的题目说明，将基本的test case填入
    # 每条case的最后一个元素是正确的结果，即期待的结果
    # 使用元组来保存输入的数据，期待的结果
    testcases = [
        ([1, 2, 4], [1, 3, 4], [1, 1, 2, 3, 4, 4]),
        ([], [], []),
        ([], [0], [0])
    ]

    for case_item in testcases:
        # 获取测试用的case和期待的执行结果

        input1 = case_item[0]
        input2 = case_item[1]

        expect = case_item[len(case_item) - 1]
        exec_result = solution.mergeTwoLists(array_to_list(input1), array_to_list(input2))

        # 判断执行结果，输出提示信息
        check_result = solution.check_result(expect, list_to_array(exec_result))
        TestcaseHelper.print_case(check_result, case_item, exec_result)
